package com.shtel.customerInfo.service;


import com.shtel.customerInfo.entity.*;
import com.shtel.customerInfo.mapper.QueryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerInfoService {
    @Autowired
    QueryMapper queryMapper;

    //查询客户基本信息
    public CustInfo queryCustInfo(String creditnum) {

        return queryMapper.queryCustInfo(creditnum);

    }


    //查询客户经理信息
    public CeoInfo queryceoinfo(String ceonumber) {

        return queryMapper.queryceoinfo(ceonumber);
    }

    //查询客户经理团队信息
    public List<CeoInfo> queryceoinfos(String ceogroupid) {

        return queryMapper.queryceoinfos(ceogroupid);


    }

    //查询政企客户信息
    public GovCustInfo querygovcustinfo(String govid) {

        return queryMapper.querygovcustinfo(govid);
    }


    //查询政企客户树(根据查询政企客户经理来)
    public GovCustNode querygovcusttree(String govid) {
        List<GovCeoInfo> govCeoInfos = queryMapper.queryGovCeoInfos(govid);
        List<GovCustNode> govCustNodes = new ArrayList<>();
        System.out.println(govCeoInfos.size());
        for (GovCeoInfo govCeoInfo : govCeoInfos) {
            GovCustNode govCustNode = new GovCustNode();
            govCustNode.setGovid(govCeoInfo.getGovid());
            govCustNode.setGovceolv(govCeoInfo.getGovceolv());
            govCustNode.setGovceoname(govCeoInfo.getGovceoname());
            govCustNode.setGovceonum(govCeoInfo.getGovceonum());
            govCustNode.setGovceoorderid(govCeoInfo.getGovceoorderid());
            govCustNode.setGovceophone(govCeoInfo.getGovceophone());
            govCustNode.setGovlowceo(govCeoInfo.getGovlowceo());
            govCustNode.setGovupceo(govCeoInfo.getGovupceo());
            govCustNodes.add(govCustNode);
        }

        List<GovCustNode> isupceo = new ArrayList<>();
        List<GovCustNode> notupceo = new ArrayList<>();
        for (GovCustNode govCustNode : govCustNodes) {
            System.out.println(govCustNode.getGovupceo());
            if (govCustNode.getGovupceo().equals("") || govCustNode.getGovupceo() == null) {
                isupceo.add(govCustNode);
            } else {
                notupceo.add(govCustNode);
            }
        }

        GovCustNode govCustNoderesult = isupceo.get(0);
        govCustNoderesult.setChild(setchild(notupceo, govCustNoderesult.getGovceonum(), govCustNoderesult));

        return govCustNoderesult;
    }

    public GovCustNode setchild(List<GovCustNode> notupceos, String govceonum, GovCustNode govCustNodes) {
        if (govCustNodes.getGovlowceo() == null || govCustNodes.getGovlowceo().equals("")) {

            return null;

        }

        GovCustNode govCustNoderesult = new GovCustNode();

        for (GovCustNode govCustNode : notupceos) {

            if (govCustNode.getGovupceo().equals(govceonum)) {
                govCustNoderesult = govCustNode;
                break;
            }
        }


        govCustNoderesult.setChild(setchild(notupceos, govCustNoderesult.getGovceonum(), govCustNoderesult));


        return govCustNoderesult;

    }

    //查询星级服务
    public StarAndScore querystarandscore(String creditnum){

        return queryMapper.querystarandscore(creditnum);

    }

    // 查询指标规则
    // public List<AlarmRule> queryIndexRule (int num){
    // Map<String, Object> returnData = new HashMap<>();
    //
    // Integer pageNum = num;
    // Integer pageSize = 5;
    // Page page = PageHelper.startPage(pageNum, pageSize, true);
    // List<AlarmRule> resultData = queryInfoMapper.queryIndexRule();
    //
    // return resultData;
    //
    // }
}
